//
//  LeftDetailTableViewController.m
//  MemoryTravel
//
//  Created by dllo on 15/12/26.
//  Copyright © 2015年 fcx. All rights reserved.
//

#import "LeftDetailTableViewController.h"

@interface LeftDetailTableViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *leftDetailTableView;

@property (nonatomic, strong) NSMutableArray *travelNotesDetailArray;
@property (nonatomic, strong) NSMutableArray *nodesArray;
@property (nonatomic, strong) NSMutableArray *allNotes;
@property (nonatomic, strong) NSMutableArray *cellCount;

@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UIImageView *personsImage;
@property (nonatomic, strong) UILabel *topNameLabel;
@property (nonatomic, strong) UILabel *topTitleLabel;

@property (nonatomic, retain) MBProgressHUD *load;

@property (strong, nonatomic) CAGradientLayer *gradientLayer;//渐变

@end

@implementation LeftDetailTableViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"iconfont-fanhui.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:@selector(leftBarButtonItemAction:)];

#pragma mark -- tableView
    self.leftDetailTableView = [[UITableView alloc]initWithFrame:[[UIScreen mainScreen] bounds]style:UITableViewStylePlain];
    self.leftDetailTableView.delegate = self;
    self.leftDetailTableView.dataSource = self;
    self.leftDetailTableView.separatorStyle = UITableViewCellSeparatorStyleNone;//取消cell线
    self.leftDetailTableView.showsVerticalScrollIndicator = NO;//取消滑块
    self.leftDetailTableView.scrollsToTop = YES;//快速回到顶部
    [self.leftDetailTableView registerClass:[LeftDetailImgeCell class] forCellReuseIdentifier:@"imageCell"];
    [self.leftDetailTableView registerClass:[LeftDetailLabelCell class] forCellReuseIdentifier:@"labelCell"];
    [self.view addSubview:self.leftDetailTableView];
    self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, Width, 200)];

#pragma mark -- 图片
    NSURL *urlHeader = [NSURL URLWithString:self.topImages];
    self.imageView.contentMode = UIViewContentModeScaleAspectFill;
    self.imageView.layer.masksToBounds = YES;
    [self.imageView sd_setImageWithURL:urlHeader placeholderImage:[UIImage imageNamed:@"placeHoderImage.png"]];
    self.imageView.userInteractionEnabled = YES;
    self.leftDetailTableView.tableHeaderView = self.imageView;

    //渐变
    self.gradientLayer = [CAGradientLayer layer];//创建
    self.gradientLayer.frame = CGRectMake(0, 100, Width, 100); //类似一张图
    [self.imageView.layer addSublayer:self.gradientLayer];//添加
    //设置渐变颜色方向  (x=0 y=1 w=0 z=0 ) 表示从上到下的渐变
    //(x=0  y=0 w=0  z=1 ) 表示从下到上的渐变
    self.gradientLayer.startPoint = CGPointMake(0, 0);//(x,y)
    self.gradientLayer.endPoint = CGPointMake(0, 1);//(w,z)
    //设定颜色组
    self.gradientLayer.colors = @[(__bridge id)[UIColor clearColor].CGColor,
                                  (__bridge id)[UIColor blackColor].CGColor];
    //设定颜色分割点 (第一个括号里 点几 表示浓度)
    self.gradientLayer.locations = @[@(0.1f) ,@(1.0f)];

#pragma mark -- 名字
    self.topNameLabel = [UILabel new];
    self.topNameLabel.text = self.namelabel;
    self.topNameLabel.font = [UIFont systemFontOfSize:15];
    self.topNameLabel.textColor = [UIColor whiteColor];
    [self.imageView addSubview:self.topNameLabel];

    [self.topNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {

        make.left.mas_equalTo(60);
        make.bottom.mas_equalTo(-35);
    }];

#pragma mark -- 标题
    self.topTitleLabel = [UILabel new];
    self.topTitleLabel.textColor = [UIColor whiteColor];
    self.topTitleLabel.text = self.titlelabel;
    self.topTitleLabel.font = [UIFont systemFontOfSize:14];
    [self.imageView addSubview:self.topTitleLabel];

    [self.topTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {

        make.left.mas_equalTo(60);
        make.bottom.mas_equalTo(-10);
    }];

#pragma mark -- 头像白边
    UIView *sideView = [UIView new];
    sideView.backgroundColor = [UIColor whiteColor];
    sideView.layer.masksToBounds = YES;
    sideView.layer.cornerRadius = 22;
    [self.imageView addSubview:sideView];

    [sideView mas_makeConstraints:^(MASConstraintMaker *make) {

        make.size.mas_equalTo(CGSizeMake(44, 44));
        make.left.mas_equalTo(10);
        make.bottom.mas_equalTo(-10);
    }];

#pragma mark -- 头像
    self.personsImage = [UIImageView new];
    self.personsImage.layer.masksToBounds = YES;
    self.personsImage.layer.cornerRadius = 20;
    [sideView addSubview:self.personsImage];
    NSURL *urlPerson = [NSURL URLWithString:self.personImage];
    [self.personsImage sd_setImageWithURL:urlPerson placeholderImage:[UIImage imageNamed:@"placeHoderImage.png"]];

    [self.personsImage mas_makeConstraints:^(MASConstraintMaker *make) {

        make.size.mas_equalTo(CGSizeMake(22, 22));
        make.edges.mas_equalTo(UIEdgeInsetsMake(3, 3, 3, 3));
    }];

#pragma mark -- 初始化数组
    self.travelNotesDetailArray = [NSMutableArray array];
    self.nodesArray = [NSMutableArray array];
    self.allNotes = [NSMutableArray array];
    self.cellCount = [NSMutableArray array];

#pragma mark -- 网络请求
    [self getAFNetWorkingData];

#pragma mark -- 菊花
    self.load = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [self.load show:YES];
    self.load.labelText = @"正在加载";
    [self.view addSubview:self.load];
}

#pragma mark -- 返回
- (void)leftBarButtonItemAction:(UIBarButtonItem *)back{

    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark -- 网络请求
- (void)getAFNetWorkingData{

    [self.allNotes removeAllObjects];
    [self.cellCount removeAllObjects];
    [self.travelNotesDetailArray removeAllObjects];

    NSString *url = [NSString stringWithFormat:@"http://chanyouji.com/api/trips/%@.json",self.pageID];
    [AppTools getDataWithUrlStr:url andBlock:^(id result) {
        NSMutableDictionary *bigDic = result;

        NSMutableArray *array = [bigDic objectForKey:@"trip_days"];

        for (NSMutableDictionary *dic in array) {

            TravelDetailModel *travelNotesDetail = [[TravelDetailModel alloc] init];

            [travelNotesDetail setValuesForKeysWithDictionary:dic];

            NSInteger count = 0;

            for (TravlesDetailModel *nodes in travelNotesDetail.nodes) {

                if (nodes.notes.count != 0) {

                    [self.allNotes addObjectsFromArray:nodes.notes];
                }

                count += nodes.notes.count;
            }

            NSString *str = [NSString stringWithFormat:@"%ld", count];

            [self.cellCount addObject:str];

            [self.travelNotesDetailArray addObject:travelNotesDetail];
        }

        [self.load hide:YES];//菊花停

        [self.leftDetailTableView reloadData];//刷新
    }];
}

#pragma mark -- 区数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return self.travelNotesDetailArray.count;
}

#pragma mark -- 行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return [[self.cellCount objectAtIndex:section] integerValue];
}

#pragma mark -- cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString * cellIdntifier = @"imageCell";

    LeftDetailImgeCell *imageCell = [tableView dequeueReusableCellWithIdentifier:cellIdntifier];

    LeftDetailLabelCell *labelCell = [tableView dequeueReusableCellWithIdentifier:@"labelCell"];

    imageCell.selectionStyle = UITableViewCellSelectionStyleNone;

    labelCell.selectionStyle = UITableViewCellSelectionStyleNone;

    TravelsDetailsModel *notes = [TravelsDetailsModel new];

    NSInteger sum = 0;

    for (int i = 0; i < indexPath.section; i++) {

        sum +=  [[self.cellCount objectAtIndex:i] integerValue];
    }

    notes = [self.allNotes objectAtIndex:sum + indexPath.row];

    if (notes.photo == nil) {

        labelCell.notes = notes;

        return labelCell;

    }else{

        imageCell.notes = notes;

        return imageCell;
    }
}

#pragma mark -- 区头天数
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{

    TravelDetailModel *travelNotesDetail = [self.travelNotesDetailArray objectAtIndex:section];

    NSString *data = [NSString stringWithFormat:@"第%ld天",travelNotesDetail.day];

    return data;
}

#pragma mark -- 计算高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    TravelsDetailsModel *notes = [[TravelsDetailsModel alloc] init];

    NSInteger sum = 0;

    for (int i = 0; i < indexPath.section; i++) {

        sum +=  [[self.cellCount objectAtIndex:i] integerValue];
    }

    notes = [self.allNotes objectAtIndex:sum + indexPath.row];

    CGFloat height;

    if (notes.photo == nil) {

        height = [LeftDetailLabelCell heightForContent:notes.imageDescription];

        return height + 20 * HEI;

    }else{

        height = [LeftDetailImgeCell heightForContent:notes.imageDescription] + (Width - 10) * HEI / [[notes.photo objectForKey:@"image_width"] floatValue] * [[notes.photo objectForKey:@"image_height"] floatValue];
        height = [LeftDetailImgeCell heightForContent:notes.imageDescription] + (Width - 10) * HEI / [[notes.photo objectForKey:@"image_width"]floatValue] * [[notes.photo objectForKey:@"image_height"]floatValue];
        
        return height + 30 * HEI;
    }
}


@end
